<?php

/**
 * core.categories class extends CatalogModule
 *
 * @todo Manufacturer filtering and column sorting have been removed from this
 * implementation. Re-implement them here? Or add to the root CatalogModule
 * class?
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 *
 * Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class CategoryCatalogModule extends CatalogModule
{
	var $namespace = 'core';
	var $extension = 'category';
	
	var $DEPTH_NESTED = 'nested';
	var $DEPTH_PRODUCTS = 'products';
	var $_categoryDepth;
	
	// SQL statements
	var $_productsInCategorySql = 'select %s from %s, %s p2c where %s and p.products_id =  p2c.products_id and p2c.categories_id = "%s"';
	
	function CategoryCatalogModule()
	{
		/**
		 * @todo $current_category_id: Where is it set?  This should ideally be
		 * moved to a class method somewhere.
		 */
		
		global $lng, $catalog, $cPath, $current_category_id, $HTTP_GET_VARS;
		
		$cPath = (isset($cPath) && tep_not_null($cPath)) ? $cPath : 0;
		$categories = $catalog->getCategories($current_category_id);
		
		if (sizeof($categories) > 0)
		{
			$this->_categoryDepth = $this->DEPTH_NESTED;
		}
		
		else
		{
			$this->_categoryDepth = $this->DEPTH_PRODUCTS;
		}
		
		$this->_sqlQuery = sprintf($this->_productsInCategorySql, $this->PRODUCT_INFO_FIELDS, $this->PRODUCT_INFO_TABLES, TABLE_PRODUCTS_TO_CATEGORIES, sprintf($this->PRODUCT_INFO_TABLES_JOIN, $lng->getLanguageId()), $current_category_id);
		
		/*
		// If manufacturer filters are set, add them here
		if (isset($HTTP_GET_VARS['manufacturers_id']))
		{
			// show the products of a specified manufacturer
			if (isset($HTTP_GET_VARS['manufacturers_id'])) {
				if (isset($HTTP_GET_VARS['filter_id']) && tep_not_null($HTTP_GET_VARS['filter_id'])) {
					// We are asked to show only a specific category
					$listing_sql = "select p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$lng->getLanguageId() . "' and p2c.categories_id = '" . (int)$HTTP_GET_VARS['filter_id'] . "'";
				} else {
					// We show them all
					$listing_sql = "select p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m where p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int)$lng->getLanguageId() . "' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'";
				}
			}
			
			if ( (!isset($HTTP_GET_VARS['sort'])) || (!ereg('[1-8][ad]', $HTTP_GET_VARS['sort'])) || (substr($HTTP_GET_VARS['sort'], 0, 1) > sizeof($column_list)) ) {
				for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
					if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
						$HTTP_GET_VARS['sort'] = $i+1 . 'a';
						$listing_sql .= " order by pd.products_name";
						break;
					}
				}
			} else {
				$sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
				$sort_order = substr($HTTP_GET_VARS['sort'], 1);
				$listing_sql .= ' order by ';
				
				switch ($column_list[$sort_col-1]) {
				case 'PRODUCT_LIST_MODEL':
					$listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
					break;
				case 'PRODUCT_LIST_NAME':
					$listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
					break;
				case 'PRODUCT_LIST_MANUFACTURER':
					$listing_sql .= "m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
					break;
				case 'PRODUCT_LIST_QUANTITY':
					$listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
					break;
				case 'PRODUCT_LIST_IMAGE':
					$listing_sql .= "pd.products_name";
					break;
				case 'PRODUCT_LIST_WEIGHT':
					$listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
					break;
				case 'PRODUCT_LIST_PRICE':
					$listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
					break;
				}
			}
			
			// optional Product List Filter
			if (PRODUCT_LIST_FILTER > 0) {
				if (isset($HTTP_GET_VARS['manufacturers_id'])) {
					$filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and p2c.categories_id = cd.categories_id and cd.language_id = '" . (int)$lng->getLanguageId() . "' and p.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "' order by cd.categories_name";
				} else {
					$filterlist_sql= "select distinct m.manufacturers_id as id, m.manufacturers_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_MANUFACTURERS . " m where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by m.manufacturers_name";
				}
				
				$filterlist_query = tep_db_query($filterlist_sql);
				
				if (tep_db_num_rows($filterlist_query) > 1) {
					echo '            <td align="center" class="main">' . tep_draw_form('filter', FILENAME_DEFAULT, 'get') . TEXT_SHOW . '&nbsp;';
					
					if (isset($HTTP_GET_VARS['manufacturers_id'])) {
						echo tep_draw_hidden_field('manufacturers_id', $HTTP_GET_VARS['manufacturers_id']);
						$options = array(array('id' => '', 'text' => TEXT_ALL_CATEGORIES));
					} else {
						echo tep_draw_hidden_field('cPath', $cPath);
						$options = array(array('id' => '', 'text' => TEXT_ALL_MANUFACTURERS));
					}
					
					echo tep_draw_hidden_field('sort', $HTTP_GET_VARS['sort']);
					
					while ($filterlist = tep_db_fetch_array($filterlist_query)) {
						$options[] = array('id' => $filterlist['id'], 'text' => $filterlist['name']);
					}
					
					echo tep_draw_pull_down_menu('filter_id', $options, (isset($HTTP_GET_VARS['filter_id']) ? $HTTP_GET_VARS['filter_id'] : ''), 'onchange="this.form.submit()"');
					echo '</form></td>' . "\n";
				}
			}
			
			$this->_sqlQuery = $listing_sql;
		}
		*/
	}
	
	/**
	 * Get the current category id being displayed
	 *
	 * @access public
	 * @return integer
	 */
	
	function getCurrentCategoryId()
	{
		global $current_category_id;
		
		if (!is_numeric($current_category_id) || $current_category_id < 1)
		{
			return -1;
		}
		
		return $current_category_id;
	}
	
	function getTemplate()
	{
		if ($this->_categoryDepth == $this->DEPTH_NESTED)
		{
			return DIR_FS_MODULES.'core.category/nested.tpl';
		}
		
		else
		{
			return CatalogModule::getTemplate();
		}
	}
	
	function getConfig()
	{
		global $lng;
		
		$langFile = DIR_FS_MODULES.$this->namespace.'.'.$this->extension.'/'.$lng->getLanguage().'.conf';
		
		if (!file_exists($langFile))
		{
			$langFile = CatalogModule::getConfig();
		}
		
		return $langFile;
	}
}

?>
